<?
/**
 * 后台管理操作类，处理后台管理的ajax响应，并调用model层处理相关操作
 * 
 * @copyright Copyright (c) 2008 sdmblog (http://www.sdmblog.cn)
 * @author sdomain(sdomain@live.cn)
 * @version admin.class.php Sun Jun 01 22:09:41 CST 2008 php $
 */
include_once('../model/admin.model.php');
include_once ('../xajax/inc/xajax.inc.php');
include_once('../include/ajax.common.php');
$xajax = new xajax('article.class.php');
//后台管理登录
$xajax->registerFunction('login');
//显示管理首页
$xajax->registerFunction('getIndexPage');
//关闭站点
$xajax->registerFunction('closeSite');
//打开站点
$xajax->registerFunction('openSite');
//显示基本设置
$xajax->registerFunction('loadSettings');
//保存基本设置
$xajax->registerFunction('saveSettings');
//显示分类设置
$xajax->registerFunction('loadCategories');
//保存分类设置
$xajax->registerFunction('saveCategories');
//显示用户组
$xajax->registerFunction('loadUserGroups');
//保存用户组
$xajax->registerFunction('saveUserGroup');
//显示表情
$xajax->registerFunction('loadSmilies');
//保存表情
$xajax->registerFunction('saveSmilies');
//显示过虑器
$xajax->registerFunction('loadWordFilter');
//保存过虑器
$xajax->registerFunction('saveWordFilter');
//显示公告
$xajax->registerFunction('loadAnnounce');
//保存公告
$xajax->registerFunction('saveAnnounce');
/**
 * 用户后台登录
 *
 * @param string $passwd 密码
 * @param string $authnum 验证码
 * @return xajaxObj
 */
function login($username,$passwd,$authnum)
{
	global $virdir,$secret_id,$webstate;
	$obj = new xajaxResponse();
	try{
		$logininfo = islogin();
		if ($logininfo==0&&$webstate==1) {
			$obj->redirect('/'.$virdir.'index.php');
			return $obj;
		}
		session_start();
		if ($authnum!=$_SESSION['auth_num']) {
			$obj->alert('验证码出错');
			$obj->assign('submit','disabled',false);
			return $obj;
		}
		if ($webstate!=1) {
			if (!isUserName($username)) {
				$obj->alert('无效/非法的用户名');
				$obj->assign('submit','disabled',false);
				return $obj;
			}else {
				include_once('../model/user.model.php');
				$user = new model_user();
				$log1 = $user->login($username,$passwd);
				if ($log1!=1) {
					$obj->alert('登录失败，可能用户名或密码出错');
					$obj->assign('submit','disabled',false);
					refreshAuthnum('authcode',$obj);
					return $obj;
				}
			}
			$uid = $username;
		}else {
			$uid = $logininfo[0];
		}
		
		$admin = new model_admin();
		$loginres = $admin->loginadmin($uid,$passwd);
		if (!$loginres) {
			$obj->alert('登录失败，可能用户名或密码出错');
			$obj->assign('submit','disabled',false);
			refreshAuthnum('authcode',$obj);
			return $obj;
		}
		$now = time();
		session_register('admininfo');
		$admininfo = $logininfo[0].'-'.$logininfo[1].'-'.$now.'-'.md5($logininfo[0].$logininfo[1].$now.$secret_id);
		$_SESSION['admininfo'] = $admininfo;
		
		$obj->redirect('/'.$virdir.'admin.php');
	}catch (Exception $e){
		alert($e->getMessage());
	}
	return $obj;
}
/**
 * 首页显示
 *
 * @return xajaxObj
 */
function getIndexPage()
{
	$obj = new xajaxResponse();
	$index = new model_admin();
	$data = $index->getServerInfo();
	$strHtml = $index->indexHtml($data);
	$obj->assign('innerContent','innerHTML',$strHtml);
	return $obj;
}
/**
 * 关闭站点
 *
 * @return xajaxObj
 */
function closeSite()
{
	$obj = new xajaxResponse();
	$close = SP('webstate',0);
	$obj->alert('站点已关闭');
	$obj->script('getIndexPage');
	return $obj;
}
/**
 * 打开站点
 *
 * @return xajaxObj
 */
function openSite()
{
	$obj = new xajaxResponse();
	$close = SP('webstate',1);
	$obj->alert('站点已打开');
	$obj->script('getIndexPage');
	return $obj;
}
/**
 * 读取基本设置
 *
 * @return xajaxObj
 */
function loadSettings()
{
	$obj = new xajaxResponse();
	try {
		$admin = new model_admin();
		$strHtml = $admin->loadSettings();
		$obj->assign('innerContent','innerHTML',$strHtml);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 保存基本设置
 *
 * @param array $settings 基本设置的表单
 * @return xajaxObj
 */
function saveSettings($settings)
{
	$obj = new xajaxResponse();
	try {
		$admin = new model_admin();
		$admin->saveSettings($settings);
		//$obj->alert(implode('-',$settings));
		$obj->alert('保存成功');
		$obj->assign('Submit','disabled',false);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
		$obj->assign('Submit','disabled',false);
	}
	return $obj;
}
/**
 * 获取分类页面
 *
 * @return xajaxObj
 */
function loadCategories()
{
	$obj=new xajaxResponse();
	try {
		$admin = new model_admin();
		$strHtml = $admin->getCategoriesPage();
		$obj->assign('innerContent','innerHTML',$strHtml);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 保存分类修改信息
 *
 * @param array $categories 分类表单数组
 * @return xajaxObj
 */
function saveCategories($categories)
{
	$obj=new xajaxResponse();
	try {
		$admin = new model_admin();
		switch ($categories['action']) {
			case 'update':
				foreach ($categories['id'] as $cid){
					if (trim($categories['name'][$cid])=="") {
						$obj->alert('请输入分类名称');
						return $obj;
					}
					$arrupdate[$cid]['name'] = $categories['name'][$cid];
					$strhidden = $categories['hidden'][$cid]=='1'?1:0;
					$arrupdate[$cid]['hidden'] = $strhidden;
					$strlocked = $categories['locked'][$cid]=='1'?1:0;
					$arrupdate[$cid]['locked'] = $strlocked;
					$strorder = intval($categories['order'][$cid]);
					$arrupdate[$cid]['indexs'] = $strorder;
				}
				//$obj->alert($arrupdate['11']['indexs']);
				$admin->updateCategiries($arrupdate);
				if (!empty($categories['new'])) {
					$categories['name'] = $categories['new'];
					$categories['hidden'] = $categories['ishidden'];
					$categories['locked'] = $categories['islocked'];
					$admin->addCategory($categories);
				}
				$strHtml = $admin->getCategoriesPage();
				$obj->alert('保存成功');
				break;
			case 'delete':
				if (is_array($categories['selected'])) {
					foreach ($categories['selected'] as $delid){
						$delid = empty($delid)?0:intval($delid);
						if (!$delid) {
							continue;
						}else {
							$admin->delCategory($delid);
						}
					}
					$obj->alert('删除完成');
					$strHtml = $admin->getCategoriesPage();
				}else {
					$obj->alert('请先择要删除的分类');
				}
				break;
			case 'move':
				//$obj->alert($categories['selected']);
				if (empty($categories['selected'])) {
					throw new Exception('请选择要移动的分类');
				}
				$froms = $admin->getCategories($categories['selected']);
				$tos = $admin->getOtherCategory($categories['selected']);
				if (empty($tos)) {
					throw new Exception('没有分类可以移动');
				}
				$strHtml = $admin->getMovetoHtml($froms,$tos);
				break;
			case 'savemoveto':
				$toid = is_numeric($categories['moveto'])?$categories['moveto']:$obj->alert('错误的目录分类');
				$admin->saveMoveTo($categories['selected'],$toid);
				$obj->alert('移动成功');
				$strHtml = $admin->getCategoriesPage();
				break;
			default:
				$obj->alert('非法请求');
				return $obj;
				break;
		}
		$obj->assign('innerContent','innerHTML',$strHtml);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	$obj->assign('Submit','disabled',false);
	return $obj;
}
/**
 * 用户组管理页面
 *
 * @return xajaxObj
 */
function loadUserGroups()
{
	$obj=new xajaxResponse();
	try {
		$admin = new model_admin();
		$data = $admin->getUserGroups();
		$strHtml = $admin->getUserGroupPage($data);
		$obj->assign('innerContent','innerHTML',$strHtml);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 用户组保存
 *
 * @param array $arrUserGroup 用户组数组
 * @return xajaxObj
 */
function saveUserGroup($arrUserGroup)
{
	$obj = new xajaxResponse();
	$admin = new model_admin();
	try {
		switch ($arrUserGroup['action']) {
			case 'update':
				$admin->saveUserGroup($arrUserGroup);
				break;
			case 'delete':
				$admin->deleteUserGroup($arrUserGroup);
				break;
			default:
				$obj->alert('非法请求');
				return $obj;
				break;
		}
		$data = $admin->getUserGroups();
		$strHtml = $admin->getUserGroupPage($data);
		$obj->assign('innerContent','innerHTML',$strHtml);
		$obj->alert('操作成功');
	}catch (Exception $e){
		$obj->alert('有错误发生：'.$e);
	}
	$obj->assign('Submit','disabled',false);
	return $obj;
}
/**
 * 显示系统表情
 *
 * @return xajaxObj
 */
function loadSmilies()
{
	$obj=new xajaxResponse();
	try {
		$admin = new model_admin();
		$data = $admin->getSmilies();
		$strHtml = $admin->getSmiliesPage($data);
		$obj->assign('innerContent','innerHTML',$strHtml);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 保存表情修改信息
 *
 * @param array $arrSmilies 表情表单数组
 * @return xajaxObj
 */
function saveSmilies($arrSmilies)
{
	$obj = new xajaxResponse();
	$admin = new model_admin();
	try {
		switch ($arrSmilies['action']) {
			case 'update':
				$admin->saveSmilies($arrSmilies);
				break;
			case 'delete':
				$admin->deleteSmilies($arrSmilies);
				break;
			default:
				$obj->alert('非法请求');
				return $obj;
				break;
		}
		$data = $admin->getSmilies();
		$strHtml = $admin->getSmiliesPage($data);
		$obj->assign('innerContent','innerHTML',$strHtml);
		$obj->alert('操作成功');
	}catch (Exception $e){
		$obj->alert('有错误发生：'.$e);
	}
	$obj->assign('Submit','disabled',false);
	return $obj;
}
/**
 * 字符串过虑页面
 *
 * @return xajaxObj
 */
function loadWordFilter()
{
	$obj=new xajaxResponse();
	try {
		$admin = new model_admin();
		$data = $admin->getWordFilter();
		$strHtml = $admin->getWordFilterPage($data);
		$obj->assign('innerContent','innerHTML',$strHtml);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
/**
 * 字符串过虑器保存
 *
 * @param array $arrWordFilter 
 * @return unknown
 */
function saveWordFilter($arrWordFilter)
{
	$obj = new xajaxResponse();
	$admin = new model_admin();
	try {
		//$obj->alert(implode('-',$arrWordFilter));
		//return $obj;
		switch ($arrWordFilter['action']) {
			case 'update':
				$admin->saveWordFilter($arrWordFilter);
				break;
			case 'delete':
				$admin->deleteWordFilter($arrWordFilter);
				break;
			default:
				$obj->alert('非法请求');
				return $obj;
				break;
		}
		$data = $admin->getWordFilter();
		$strHtml = $admin->getWordFilterPage($data);
		$obj->assign('innerContent','innerHTML',$strHtml);
		$obj->alert('操作成功');
	}catch (Exception $e){
		$obj->alert('有错误发生：'.$e->getMessage());
	}
	$obj->assign('Submit','disabled',false);
	return $obj;
}
function loadAnnounce()
{
	$obj=new xajaxResponse();
	try {
		$admin = new model_admin();
		$strHtml = $admin->getAnnouncePage();
		$obj->assign('innerContent','innerHTML',$strHtml);
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	return $obj;
}
function saveAnnounce($arrAnnounce)
{
	$obj=new xajaxResponse();
	try {
		//$obj->alert('sdm');
		//return $obj;
		$admin = new model_admin();
		$admin->saveAnnounce($arrAnnounce);
		$strHtml = $admin->getAnnouncePage();
		$obj->assign('innerContent','innerHTML',$strHtml);
		$obj->alert('操作成功');
	}catch (Exception $e){
		$obj->alert($e->getMessage());
	}
	$obj->assign('Submit','disabled',false);
	return $obj;
}
$xajax->processRequest();
?>